//
// Created by wenbo on 24-9-7.
//

#ifndef __DRV_MRC_H__
#define __DRV_MRC_H__

#ifdef __cplusplus
extern "C"
{
#endif

#include "tim.h"
#include "usart.h"
#include "adc.h"
#include "main.h"

// #define AT8236
#define VNH7040

    typedef enum
    {
        Disengagement,
        Engagement,
        Demagnetization,
        Magnetization

    } MRC_State;

    typedef enum
    {
        NONE = 0,
        UNLOCK = 1,
        LOCK = 2
    } MR_Command;

#pragma pack(1)
    /**
     * @brief Structure representing the MR device.
     *
     * This structure holds various parameters and function pointers related to the MR device.
     */
    typedef struct Device_MRC_t
    {
        MRC_State state; // State of the MR device

        void (*init)(TIM_HandleTypeDef, uint32_t, uint32_t, uint16_t); // Function pointer to initialize the MR device
        const uint8_t *device_name;                                    // Pointer to the device name
        TIM_HandleTypeDef *pwm_handle;                                 // Pointer to the TIM_HandleTypeDef structure
#ifdef AT8236
        uint32_t IN1; // Input 1
        uint32_t IN2; // Input 2
#endif

#ifdef VNH7040

        GPIO_TypeDef *INA;
        uint32_t INA_PIN;

        GPIO_TypeDef *INB;
        uint32_t INB_PIN;

        uint32_t PWM; // PWM
#endif

        int (*LOG_MSG)(const char *format, ...); // Function pointer to log messages
        float des_voltage;                       // Voltage of the MR device
        uint8_t STATE_UPDATE_FLAG;               // Flag to update the state
        uint8_t demagnetization_counter;         // Counter for demagnetization
        uint8_t magnetization_counter;

        MR_Command cmd;

        float actual_voltage; // Counter for magnetization
    } Device_MRC_t;
#pragma pack()

    void MRC_set_pwm_param(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t freq, uint16_t duty);

#ifdef AT8236
    __WEAK void MRC_Init(const uint8_t *dev_name, Device_MRC_t *MRC, TIM_HandleTypeDef *htim, uint32_t IN1, uint32_t IN2);
#endif

#ifdef VNH7040
    void MRC_Init(const uint8_t *dev_name, Device_MRC_t *MRC, TIM_HandleTypeDef *htim,
                  GPIO_TypeDef *INA, uint16_t INA_PIN,
                  GPIO_TypeDef *INB, uint16_t INB_PIN,
                  uint32_t PWM);
    void VNH7070_Multisense_ADC_process(Device_MRC_t *MRC1, Device_MRC_t *MRC2, float *ADC_BUFFER);
#endif

    void MRC_set_voltage(float des_voltage, Device_MRC_t *MRC);
    void MRC_lock(Device_MRC_t *MRC);
    void MRC_unlock(Device_MRC_t *MRC);
    void MRC_switch(Device_MRC_t *MRC, TIM_HandleTypeDef *htim);

#define DEMAGNETIZATION_BUFFER_SIZE 255 // Define the size of the demagnetization buffer
#define MAGNETIZATION_BUFFER_SIZE 255   // Define the size of the magnetization buffer

    extern float demagnetization_buffer[];
    extern float magnetization_buffer[];
    // extern Device_MRC_t MRC1;
    // extern Device_MRC_t MRC2;

#ifdef __cplusplus
}
#endif

#endif
